﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Pikapika.Filters
{
    public class ActionExecuteTimeFilterAttribute : ActionFilterAttribute
    {

        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            base.OnActionExecuting(filterContext);
            filterContext.Controller.ViewBag.ControllerExecutionStartTime = DateTime.UtcNow;
        }

        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            base.OnActionExecuted(filterContext);
            dynamic viewModel = filterContext.Controller.ViewBag;
            viewModel.ControllerExecutionEndTime = DateTime.UtcNow;
            viewModel.ControllerExecutionTime = DateTime.UtcNow - (DateTime)viewModel.ControllerExecutionStartTime;
        }

        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            base.OnResultExecuting(filterContext);
            filterContext.Controller.ViewBag.ResultExecutionStartTime = DateTime.UtcNow;
        }
    }
}